﻿/******************************************************************************************
 * 对于一个商业软件来说，授权码这个功能必不可少。我这里采用CPU序列号加硬盘标识来判断是否授权。完整代码如下：
 * https://blog.csdn.net/qq_34090937/article/details/102699065
 * 
 * 其他   --- c# 代码授权怎么实现
 * https://blog.csdn.net/cuoban/article/details/50492895?utm_medium=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-1.add_param_isCf&depth_1-utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-1.add_param_isCf
 *******************************************************************************************/

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Management;

namespace LicenseGenerateToolV2
{
    public partial class FrmShouQuan : Form
    {
        public FrmShouQuan()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 获取CPU的参数
        /// </summary>
        /// <returns></returns>
        public string getCpu()
        {
            string strCpu = null;
            ManagementClass myCpu = new ManagementClass("win32_Processor");
            ManagementObjectCollection myCpuConnection = myCpu.GetInstances();
            foreach (ManagementObject myObject in myCpuConnection)
            {
                strCpu = myObject.Properties["Processorid"].Value.ToString();
                break;
            }
            return strCpu;
        }

        /// <summary>
        /// 获取硬盘的参数
        /// </summary>
        /// <returns></returns>
        public string GetDiskVolumeSerialNumber()
        {
            ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
            ManagementObject disk = new ManagementObject("win32_logicaldisk.deviceid=\"c:\"");
            disk.Get();
            return disk.GetPropertyValue("VolumeSerialNumber").ToString();
        }

        /// <summary>
        /// 生成机器码，就是CPU参数加上硬盘参数
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {
            txtMachineInfo.Text = getCpu() + GetDiskVolumeSerialNumber();//获得24位Cpu和硬盘序列号
        }

        public int[] intCode = new int[127];//用于存密钥
        public void setIntCode()
        {
            for (int i = 1; i < intCode.Length; i++)
            {
                intCode[i] = i % 10;
            }
        }
        public int[] intNumber = new int[25];//用于存机器码的Ascii值
        public char[] Charcode = new char[25];//存储机器码字

        //生成注册码
        private void button2_Click(object sender, EventArgs e)
        {
            if (txtMachineInfo.Text != "")
            {
                txtRegistText.Text = GetResistText(txtMachineInfo.Text);
            }
            else
            { MessageBox.Show("请选生成机器码", "注册提示"); }
        }

        /// <summary>
        /// 根据机器码获取注册码
        /// </summary>
        /// <param name="machineText"></param>
        /// <returns></returns>
        private string GetResistText(string machineText)
        {
            //把机器码存入数组中
            setIntCode();//初始化127位数组
            for (int i = 1; i < Charcode.Length; i++)//把机器码存入数组中
            {
                Charcode[i] = Convert.ToChar(machineText.Substring(i - 1, 1));
            }
            for (int j = 1; j < intNumber.Length; j++)//把字符的ASCII值存入一个整数组中。
            {
                intNumber[j] = intCode[Convert.ToInt32(Charcode[j])] + Convert.ToInt32(Charcode[j]);

            }
            string strAsciiName = null;//用于存储机器码
            for (int j = 1; j < intNumber.Length; j++)
            {
                if (intNumber[j] >= 48 && intNumber[j] <= 57)//判断字符ASCII值是否0－9之间
                {
                    strAsciiName += Convert.ToChar(intNumber[j]).ToString();
                }
                else if (intNumber[j] >= 65 && intNumber[j] <= 90)//判断字符ASCII值是否A－Z之间
                {
                    strAsciiName += Convert.ToChar(intNumber[j]).ToString();
                }
                else if (intNumber[j] >= 97 && intNumber[j] <= 122)//判断字符ASCII值是否a－z之间
                {
                    strAsciiName += Convert.ToChar(intNumber[j]).ToString();
                }
                else//判断字符ASCII值不在以上范围内
                {
                    if (intNumber[j] > 122)//判断字符ASCII值是否大于z
                    { strAsciiName += Convert.ToChar(intNumber[j] - 10).ToString(); }
                    else
                    {
                        strAsciiName += Convert.ToChar(intNumber[j] - 9).ToString();
                    }

                }
            }
            return strAsciiName;
        }

        private void btnRegist_Click(object sender, EventArgs e)
        {
            if (txtRegistText.Text != "")
            {
                if (textBox1.Text.TrimEnd().Equals(txtRegistText.Text.TrimEnd()))
                {
                    //这里需要将机器码和注册码保存到数据库或注册表中，以便以后校验（推荐保存到数据库中，这样不怕重装系统）
                    MessageBox.Show("注册成功");
                }
                else
                {
                    MessageBox.Show("注册码输入错误");

                }

            }
            else { MessageBox.Show("请生成注册码", "注册提示"); }

        }

        /// <summary>
        /// 验证是否已经激活软件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button4_Click(object sender, EventArgs e)
        {
            //这里重新生成机器码和注册码，与数据库中进行对比。只要有一个参数不一致，就是未激活（此处省略两万行代码）
        }
    }
}
